<template>
  <div class="case">
    <na-table :datas.sync="tableData" :searchObj.sync="search" @updateDatas="getAllMaterils" @newAdd="newAddClick">
    </na-table>
    <!-- 人员寻找弹框   -->
    <per-redio-dialog :per-visible.sync="isperShow" @getEmployee="getEmployee" />
    <el-dialog :title="tname" :visible.sync="isAddProduct" :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog" width="85%" @close="closeDialog" @open="openCp" destroy-on-close>
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"><span class="mr5">报废单号：</span>{{ ProductForm.scrapBillNo || "系统自动生成" }}</span>
          <span class="mr15"><span
              class="mr5">创建日期：</span>{{ ProductForm.createTime || dayjs().format("YYYY-MM-DD") }}</span>
          <span><span class="mr5">创建人员：</span>{{ opsid != 1 ? ProductForm.createPersonName : name }}</span>
        </div>
        <div class="addWuliaoBox h100">
          <el-form ref="ProductForm" :model="ProductForm" :rules="rules" label-width="110px" label-position="left"
            class="dialogForm dialogInlineForm h100" :disabled="canDiable">
            <el-row class="h100">
              <el-col class="mt15 h100 boxleft">
                <el-row>
                  <el-col :span="8">
                    <el-form-item label="报废类型" prop="scrapTypeName">
                      <el-input v-model="ProductForm.scrapTypeName" placeholder="报废类型" size="mini" disabled />
                      <!-- <el-select
                        filterable v-model="ProductForm.getMaterialType" placeholder="领料类型" size="mini">
                    <el-option
                      v-for="item in purchaseTypes"
                      :key="item.dicValueID"
                      :label="item.dicValueDesc"
                      :value="item.dicValueCode"
                    />
                  </el-select> -->
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item label="报废人员" prop="scrapPersonName">
                      <div class="flex flex_center">
                        <div class="flex1">
                          <el-input v-model="ProductForm.scrapPersonName" size="mini" placeholder="请选择人员"
                            :disabled="canDiable">
                            <el-button slot="append" icon="el-icon-user" :disabled="canDiable"
                              @click="isperShow = true" />
                          </el-input>
                        </div>
                      </div>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="报废备注" class="scrapRemark">
                  <el-input v-model="ProductForm.scrapRemark" type="textarea" :rows="1" placeholder="报废备注" size="mini"
                    clearable />
                </el-form-item>
                <div class="tableBox">
                  <div class="tt flex flex_between">
                    <span class="fw">报废明细</span>
                    <span v-if="!canDiable" class="c_3F61F1 mr15 pointer" @click="addMX"><i class="el-icon-plus mr5" />
                      选择成品</span>
                  </div>
                  <div ref="bigdialogTable" class="bigdialogTbCon">
                    <el-table ref="multipleTable" :data="ProductForm.mxDataList" border fit style="width: 100%"
                      :expand-row-keys="expends" :row-key="getRowKeys">
                      <el-table-column type="expand">
                        <template slot-scope="{ row }">
                          <div class="dialogExpandTable">
                            <el-table :data="row.packageDataList || []"
                              :header-cell-style="{ background: '#3f61f110' }">
                              <el-table-column prop="packageGuiGeName" label="纸箱规格" min-width="90" />
                              <el-table-column prop="packageDescription" label="包装描述" min-width="90" />
                              <el-table-column prop="eachBoxQuantityStr" label="每箱数量" min-width="90" />
                              <el-table-column v-if="opsid == 1 || opsid == 4 || opsid == 5" prop="stockBoxCount"
                                label="库存箱数" min-width="90" />
                              <el-table-column v-if="opsid == 1 || opsid == 4 || opsid == 5" prop="stockQuantity"
                                label="库存数量" min-width="90">
                                <!-- <template slot-scope="scope">
                                  <div class="flex">
                                    <template>
                                      <span class="mr5" style="color: #E13335">{{ scope.row.stockQuantity }}</span>
                                    </template>
                                  </div>
                                </template> -->
                              </el-table-column>
                              <el-table-column label="报废箱数" prop="scrapBoxAmount" show-overflow-tooltip min-width="90">
                                <template slot-scope="{ row }">
                                  <el-input v-model.number="row.scrapBoxAmount" :disabled="canDiable" placeholder="请输入"
                                    size="mini" @blur="statistics" type="number" />
                                </template>
                              </el-table-column>
                              <el-table-column label="报废数量" prop="scrapQuantity" show-overflow-tooltip min-width="90">
                                <template slot-scope="{ row }">
                                  <el-input v-model.number="row.scrapQuantity" :disabled="canDiable" placeholder="请输入"
                                    size="mini" @blur="statistics" type="number" />
                                </template>
                              </el-table-column>
                              <el-table-column label="备注" prop="scrapMxRemark" show-overflow-tooltip min-width="90">
                                <template slot-scope="{ row }">
                                  <el-input v-model="row.scrapMxRemark" :disabled="canDiable" placeholder="请输入"
                                    size="mini" />
                                </template>
                              </el-table-column>
                              <el-table-column v-if="!canDiable" label="操作" width="auto" align="center">
                                <template slot-scope="scope">
                                  <el-tooltip class="item" effect="dark" content="删除" placement="right-start">
                                    <span class="pointer" @click="
                                        zxDelete(
                                          row.packageDataList,
                                          scope.$index
                                        )
                                      "><i class="el-icon-delete" /></span>
                                  </el-tooltip>
                                </template>
                              </el-table-column>
                            </el-table>
                          </div>
                        </template>
                      </el-table-column>
                      <el-table-column prop="productNo" label="成品编号" width="120" />
                      <el-table-column prop="productTypeName" label="成品类型" width="120" />
                      <el-table-column prop="productName" label="产品名称" width="auto" />
                      <el-table-column prop="printingSurface" label="版面" width="120" />
                      <el-table-column prop="productVersion" label="版本" width="120" />
                      <el-table-column v-if="opsid == 1 || opsid == 4 || opsid == 5" prop="sumBoxCount" label="库存箱数"
                        width="120" />
                      <el-table-column v-if="opsid == 1 || opsid == 4 || opsid == 5" prop="sumStockQuantity"
                        label="库存数量" width="120">
                        <!-- <template slot-scope="scope">
                          <div class="flex">
                            <template>
                              <span class="mr5" style="color: #E13335">{{ scope.row.sumStockQuantity }}</span>
                            </template>
                          </div>
                        </template> -->
                      </el-table-column>
                      <el-table-column prop="scrapTotalBox" label="报废总箱数" width="120" />
                      <el-table-column prop="scrapTotalSum" label="报废总数量" width="120" />
                      <el-table-column prop="productUnit" label="单位" width="60" />

                      <el-table-column v-if="!canDiable" label="操作" width="auto" align="center">
                        <template slot-scope="scope">
                          <el-tooltip class="item" effect="dark" content="删除" placement="right-start">
                            <span class="pointer" @click="purchaseDelete(scope.$index)"><i
                                class="el-icon-delete" /></span>
                          </el-tooltip>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                </div>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isAddProduct = false">取 消</el-button>
        <!-- 新增       -->
        <el-button v-if="opsid !== 2" :loading="addProductLoading" :disabled="addProductLoading"
          element-loading-spinner="el-icon-loading" type="primary" size="mini" @click="savell">{{ btm }}</el-button>
      </div>
    </el-dialog>
    <!-- 选择成品 -->
    <el-dialog append-to-body title="选择成品" :visible.sync="isSemidialog" :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog" width="70%" @close="closeSemiDialog" @open="openmater">
      <div class="dialogDody marDialog flex flex_column">
        <div class="btnCon flex flex_end">
          <!--      搜索框-->
          <div class="search">
            <el-input v-model="SemiSearch.likeWords" placeholder="输入关键字" size="small">
              <el-button slot="append" :loading="marLoading" type="primary" icon="el-icon-search" :disabled="marLoading"
                element-loading-spinner="el-icon-loading" @click="openmater">
                搜索
              </el-button>
            </el-input>
          </div>
        </div>
        <!-- 表格区域       -->
        <div class="tableCon not">
          <el-table ref="multipleTable" :data="dataList" border fit style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="{ row }">
                <div class="dialogExpandTable">
                  <el-table :data="row.productPackageList || []" :header-cell-style="{ background: '#3f61f110' }"
                    @selection-change="handlecpChange($event, row)">
                    <el-table-column prop="selection" width="55">
                      <el-table-column type="selection" width="55" />
                      <el-table-column prop="cartonSizeName" label="纸箱规格" min-width="90" />
                      <el-table-column prop="packageDescription" label="包装描述" min-width="90" />
                      <el-table-column prop="eachBoxQuantityStr" label="包装数量" min-width="90" />
                      <el-table-column prop="stockBoxCount" label="库存箱数" min-width="90" />
                      <el-table-column prop="stockQuantity" label="库存数量" min-width="90" />
                    </el-table-column>
                  </el-table>
                </div>
              </template>
            </el-table-column>
            >
            <el-table-column prop="productNo" label="成品编号" width="120" /> >
            <el-table-column prop="productTypeName" label="成品类型" width="120" />
            >
            <el-table-column prop="customerName" label="	所属客户" width="180" />
            >
            <el-table-column prop="productName" label="产品名称" width="220" />
            >
            <el-table-column prop="printingSurface" label="版面" width="120" />
            >
            <el-table-column prop="productVersion" label="版本" width="120" /> >
            <el-table-column prop="sumBoxCount" label="库存总箱数" width="120" />
            <el-table-column prop="sumStockQuantity" label="库存总数量" width="120" />
            >
            <el-table-column prop="productUnit" label="单位" width="auto" />
          </el-table>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isSemidialog = false">取 消</el-button>
        <el-button v-loading="addLoading" :disabled="addLoading" element-loading-spinner="el-icon-loading"
          type="primary" size="mini" @click="saveCp">
          确定</el-button>
      </div>
    </el-dialog>
    <!-- 原因   -->
    <el-dialog title="作废原因" :visible.sync="isNopassshow" :close-on-click-modal="false" custom-class="comDialog"
      width="40%" @close="closePassDialog">
      <div class="dialogDody">
        <el-form ref="passForm" :model="passForm" class="dialogForm">
          <el-form-item class="mt12">
            <el-input v-model="passForm.cancelReason" prop="cancelReason" type="textarea" placeholder="请输入内容"
              :autosize="{ minRows: 6 }" size="mini" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isNopassshow = false">取 消</el-button>
        <el-button :disabled="addLoading" element-loading-spinner="el-icon-loading" type="primary" size="mini"
          @click="passSave">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {
    arraySort
  } from "@/utils";
  import naTable from "@/components/naTable";
  import perRedioDialog from "@/components/perDialogRedio";
  import SelectLLorder from "@/components/selectLLorder";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "Materiel",
    components: {
      naTable,
      perRedioDialog,
      SelectLLorder,
    },
    data() {
      return {
        ops: "add", // 操作、新建，修改，查看
        opsid: 1, // 3 确定 1 新建， 4 修改， 2 查看
        tableHeight: null, // 表格最大高度
        showOrder: false,
        isSeeMaterriel: false, // 查看弹框是否打开
        isAddMaterriel: false, // 新建弹框是否打开
        addMaterrielLoading: false, // 新建保存加载
        searchLoading: false, // 搜索加载效果
        allMaterirls: [], // 全部数据列表
        colorState: null,
        isCustomerInfo: false,
        addLableLoading: false,
        isAdressDialog: false,
        expends: [],
        cusLoading: false,
        radioCusSelect: null,
        currentCuslistRow: null,
        cusSearch: {
          likeWords: "",
        },
        addLoading: false,
        selectRow: null,
        shippingAddres: [],
        isNopassshow: false,
        ReceivingTypeList: [],
        SendingTypeList: [],
        LogisticsPaidTypeList: [],
        FYDepartmentList: [],
        equipmentList: [],
        option: {
          disabledDate: (time) => {
            return time.getTime() < Date.now() - 1 * 24 * 3600 * 1000;
          },
        },
        passForm: {
          cancelReason: "",
        },
        // 表格操作
        ProductForm: {
          scrapTypeName: "成品报废",
          scrapPersonName: "",
          scrapPerson: "",
          scrapType: 1,
          mxDataList: [],
        },
        // 搜索条件
        search: {
          QueryDataBaseFiled: "",
          pageIndex: 1,
          pageSize: 30,
          scrapType: 1,
          scrapBillState: -1,
          outStockState: -1,
          likeWords: "",
        },
        total: 0, // 总条数
        tableHead: [], // 表格表头
        radioContractSelect: null, // 客户合同单选值
        addProductLoading: false,
        isAddProduct: false,
        isperShow: false,
        isSemidialog: false,
        contractLists: [],
        multipleSelection: null,
        purchaseInfoDatas: [{}],
        cusDataLists: [],
        allSupliers: [],
        shippingAddress: [],
        allAddress: [],
        selectcpRow: null,
        dataList: [],
        purchaseTypes: [{
            dicValueID: 0,
            dicValueDesc: "生产领料",
            dicValueCode: 1,
          },
          {
            dicValueID: 1,
            dicValueDesc: "生产补料",
            dicValueCode: 2,
          },
        ],
        SemiSearch: {
          likeWords: "",
          productType: 1,
          productNo: "",
        },
        materialList: [],
        marLoading: false,
        canDiable: false,
        rules: {
          scrapPersonName: [{
            required: true,
            message: "请选择人员",
            trigger: "change"
          }, ],
        },

        tableData: {
          headId: 'head', //表头列显隐组件所需ID-避免多个页面id重复问题
          tableId: 'scrapBillID', //表格列表key,也就是一条数据的唯一标识
          senior: false, //高级搜索是否显示
          disableWord: '', //是否禁用显示的字段
          disableStatus: '', //判断未禁用的状态值
          // addTitle: '', //新建按钮名称-为''则不显示，为undefine则为新建
          addPermission: 'MenuCode5-7-2-1', //新建按钮权限
          addIcon: '', //新建按钮icon
          voidWord: '', //需要查询作废的字段
          statusList: [], //状态筛选条件列表
          statusWord: '', //状态筛选的字段
          refresh: true, //刷新按钮
          showSum: false, //显示合计
          //表格按钮
          operationBtn: [{
              title: '查看', //按钮名称
              methodsName: 'handleClick', //按钮绑定方法的字段
            },
            {
              title: '修改', //按钮名称
              methodsName: 'handleUpdata', //按钮绑定方法的字段
              permission: "MenuCode5-7-2-2", //按钮权限
              hideCondition: { //按钮显隐条件
                prop: 'scrapBillState', //控制显隐的状态字段
                show: [ //有哪些状态可以使用-值必须跟状态的类型保持一致
                  0,
                ],
              },
            },
            {
              title: '确认', //按钮名称
              methodsName: 'handleConfirm', //按钮绑定方法的字段
              permission: "MenuCode5-7-2-3", //按钮权限
              hideCondition: { //按钮显隐条件
                prop: 'scrapBillState', //控制显隐的状态字段
                show: [ //有哪些状态可以使用-值必须跟状态的类型保持一致
                  0,
                ],
              },
            },
            {
              title: '作废', //按钮名称
              methodsName: 'handleDone', //按钮绑定方法的字段
              permission: "MenuCode5-7-2-5", //按钮权限
              hideCondition: { //按钮显隐条件
                prop: 'isVoid', //控制显隐的状态字段
                show: [ //有哪些状态可以使用-值必须跟状态的类型保持一致
                  0,
                ],
              },
            },
            {
              title: '删除', //按钮名称
              methodsName: 'handleDelete', //按钮绑定方法的字段
              permission: "MenuCode5-7-2-4", //按钮权限
              hideCondition: { //按钮显隐条件
                prop: 'scrapBillState', //控制显隐的状态字段
                show: [ //有哪些状态可以使用-值必须跟状态的类型保持一致
                  0,
                ],
              },
            },
            {
              title: '复制', //按钮名称
              methodsName: 'handleCopy', //按钮绑定方法的字段
              permission: "MenuCode5-7-2-6", //按钮权限
              hideCondition: { //按钮显隐条件
                prop: 'scrapBillState', //控制显隐的状态字段
                show: [ //有哪些状态可以使用-值必须跟状态的类型保持一致
                  -1,
                ],
              },
            },
          ],
          //表格内容
          tbody: [],
          //表格表头
          thead: [],
        },
      };
    },
    computed: {
      ...mapGetters(["name", "userInfo"]),
      btm() {
        if (this.opsid === 3) {
          return "确认";
        } else {
          return "保存";
        }
      },
      tname() {
        if (this.opsid === 1) {
          return "新建成品报废";
        } else if (this.opsid === 2) {
          return "查看成品报废";
        } else if (this.opsid === 3) {
          return "确定成品报废";
        } else if (this.opsid === 4) {
          return "修改成品报废";
        } else {
          return "复制成品报废";
        }
      },
    },
    created() {
      //  所有列表数据
      this.getAllMaterils();
      // 表格得表头
      this.getTableHead();
    },
    mounted() {
    },
    methods: {
      getExpends() {
        var productNo = this.ProductForm.mxDataList.map((e) => e.productNo);
        this.expends = productNo;
      },
      addMX() {
        this.isSemidialog = true;
      },
      getRowKeys(row) {
        return row.productNo;
      },
      closePassDialog() {
        this.passForm.cancelReason = "";
        this.selectRow = null;
      },
      // 新建点击事件
      newAddClick() {
        this.ops = "add";
        this.opsid = 1;
        this.isAddProduct = true;
        this.canDiable = false;
      },
      handleClick(row) {
        this.opsid = 2;
        this.canDiable = true;
        this.getDetail(row);
      },
      handleConfirm(row) {
        this.opsid = 3;
        this.canDiable = true;
        this.getDetail(row);
      },
      handleDone(row) {
        this.selectRow = row;
        this.isNopassshow = true;
      },
      handleCopy(row) {
        this.opsid = 5;
        this.canDiable = false;
        this.openCp();
        const _data = {
          queryBy: 1,
          queryValue: row.id,
        };
        this.$api.warehouse.GetSingleProduceLogistics(_data).then((res) => {
          delete res.id;
          delete res.code;
          this.ProductForm = res;
          this.isAddProduct = true;
        });
      },
      handleUpdata(row) {
        this.opsid = 4;
        this.canDiable = false;
        this.getDetail(row);
      },
      passSave() {
        const _data = {
          entityID: this.selectRow.id,
        };
        this.$api.warehouse
          .VoidProduceLogistics(_data)
          .then(() => {
            this.getAllMaterils();
            this.isNopassshow = false;
            this.addLoading = false;
            this.$message({
              message: "作废成功",
              type: "success",
            });
          })
          .catch(() => {});
      },
      handleDelete(row) {
        const _data = {
          scrapBillID: row.scrapBillID,
        };
        this.$confirm("确定删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          this.$api.warehouse
            .DeleteFinishedProductScrapBill(_data)
            .then(() => {
              this.getAllMaterils();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            })
            .catch(() => {});
        });
      },
      getDetail(row) {
        const _data = {
          scrapBillID: row.scrapBillID,
        };
        this.$api.warehouse
          .GetFinishedProductScrapBillDetail(_data)
          .then((res) => {
            res.mxDataList.forEach((e) => {
              e.packageDataList = e.productPackageList;
              e.productPackageList.forEach((e2) => {
                e2.packageGuiGeName = e2.cartonSizeName;
              });
            });
            res.baseData.mxDataList = res.mxDataList;
            this.ProductForm = res.baseData;
            this.getExpends();
            this.statistics();
            this.isAddProduct = true;
          });
      },
      purchaseDelete(index) {
        this.ProductForm.mxDataList.splice(index, 1);
      },
      zxDelete(row, index) {
        row.splice(index, 1);
      },
      // 选择成品
      openmater() {
        this.$api.warehouse
          .GetMatchFinishedProductDataList(this.SemiSearch)
          .then((res) => {
            this.dataList = res;
            this.searchLoading = false;
          })
          .catch(() => {
            this.searchLoading = false;
          });
      },
      openCp() {
        this.shippingAddress = [];
      },
      savell() {
        if (this.opsid === 3) {
          const _data3 = {
            scrapBillID: this.ProductForm.scrapBillID,
          };
          this.$api.warehouse
            .SureFinishedProductScrapBill(_data3)
            .then(() => {
              this.addProductLoading = false;
              this.isAddProduct = false;
              this.getAllMaterils();
              this.$message({
                message: "确认成功",
                type: "success",
              });
            })
            .catch(() => {
              this.addProductLoading = false;
            });
        } else if (this.opsid === 4) {
          let _flag = false;
          const parmas = this.ProductForm;
          if (
            !this.ProductForm.mxDataList ||
            this.ProductForm.mxDataList.length === 0
          ) {
            this.$message({
              message: "请选择报废明细",
              type: "error",
            });
          } else {
            for (
              let index = 0; index < this.ProductForm.mxDataList.length; index++
            ) {
              for (
                let index2 = 0; index2 <
                this.ProductForm.mxDataList[index].packageDataList.length; index2++
              ) {
                const _scrapBoxAmount =
                  this.ProductForm.mxDataList[index].packageDataList[index2]
                  .scrapBoxAmount;
                const _scrapQuantity =
                  this.ProductForm.mxDataList[index].packageDataList[index2]
                  .scrapQuantity;
                if (!_scrapBoxAmount || !_scrapQuantity) {
                  this.$message({
                    message: "请输入报废数和报废箱数",
                    type: "warning",
                  });
                  break;
                } else {
                  if (
                    this.ProductForm.mxDataList[index].packageDataList[index2]
                    .scrapQuantity >
                    this.ProductForm.mxDataList[index].packageDataList[index2]
                    .stockQuantity
                  ) {
                    this.ProductForm.mxDataList[index].packageDataList[
                        index2
                      ].scrapQuantity =
                      this.ProductForm.mxDataList[index].packageDataList[
                        index2
                      ].stockQuantity;
                    this.$message({
                      message: "报废数量不能大于库存数量",
                      type: "warning",
                    });
                    break;
                  } else {
                    _flag = true;
                  }
                }
              }
            }
            if (_flag) {
              this.$refs.ProductForm.validate((valid) => {
                if (valid) {
                  this.addProductLoading = true;
                  this.$api.warehouse
                    .UpdateFinishedProductScrapBill(parmas)
                    .then(() => {
                      this.addProductLoading = false;
                      this.isAddProduct = false;
                      this.getAllMaterils();
                      this.$message({
                        message: "修改成功",
                        type: "success",
                      });
                    })
                    .catch(() => {
                      this.addProductLoading = false;
                    });
                }
              });
            }
          }
        } else {
          let _flag = false;
          const parmas = this.ProductForm;
          if (
            !this.ProductForm.mxDataList ||
            this.ProductForm.mxDataList.length === 0
          ) {
            this.$message({
              message: "请选择报废明细",
              type: "error",
            });
          }
          for (
            let index = 0; index < this.ProductForm.mxDataList.length; index++
          ) {
            for (
              let index2 = 0; index2 < this.ProductForm.mxDataList[index].packageDataList.length; index2++
            ) {
              const _scrapBoxAmount =
                this.ProductForm.mxDataList[index].packageDataList[index2]
                .scrapBoxAmount;
              const _scrapQuantity =
                this.ProductForm.mxDataList[index].packageDataList[index2]
                .scrapQuantity;
              if (!_scrapBoxAmount || !_scrapQuantity) {
                this.$message({
                  message: "请输入报废数和报废箱数",
                  type: "warning",
                });
                break;
              } else {
                if (
                  this.ProductForm.mxDataList[index].packageDataList[index2]
                  .scrapQuantity >
                  this.ProductForm.mxDataList[index].packageDataList[index2]
                  .stockQuantity
                ) {
                  this.ProductForm.mxDataList[index].packageDataList[
                      index2
                    ].scrapQuantity =
                    this.ProductForm.mxDataList[index].packageDataList[
                      index2
                    ].stockQuantity;
                  this.$message({
                    message: "报废数量不能大于库存数量",
                    type: "warning",
                  });
                  break;
                } else {
                  _flag = true;
                }
              }
            }
          }
          if (_flag) {
            this.$refs.ProductForm.validate((valid) => {
              if (valid) {
                this.addProductLoading = true;
                this.$api.warehouse
                  .CreateNewFinishedProductScrapBill(parmas)
                  .then(() => {
                    this.addProductLoading = false;
                    this.isAddProduct = false;
                    this.getAllMaterils();
                    this.$message({
                      message: "保存成功",
                      type: "success",
                    });
                  })
                  .catch(() => {
                    this.addProductLoading = false;
                  });
              }
            });
          }
        }
      },

      // 得到申请人员
      getEmployee(val) {
        this.ProductForm.scrapPersonName = val.employeeName;
        this.ProductForm.scrapPerson = val.employeeCode;
      },
      supplierChange(val) {
        this.materSearch.supplierCode = val;
        this.purchaseInfoDatas = [];
      },
      closeSemiDialog() {
        this.isSemidialog = false;
      },
      MarterdialogShow() {},

      //统计
      statistics() {
        let arr = JSON.parse(JSON.stringify(this.ProductForm.mxDataList));
        arr.forEach((i) => {
          i.scrapTotalBox = 0;
          i.scrapTotalSum = 0;
          i.packageDataList.forEach((j) => {
            if (j.scrapBoxAmount) {
              i.scrapTotalBox += j.scrapBoxAmount;
            }
            if (j.scrapQuantity) {
              i.scrapTotalSum += j.scrapQuantity;
            }
          });
        });
        this.$forceUpdate();
        this.ProductForm.mxDataList = arr;
        console.log(this.ProductForm.mxDataList);
      },

      handlecpChange(event, row) {
        const _row = row;
        _row.packageDataList = event;
        event.forEach((e) => {
          e.packageGuiGeName = e.cartonSizeName;
        });
        this.selectcpRow = _row;
      },
      saveCp() {
        const array = JSON.parse(JSON.stringify(this.ProductForm.mxDataList));
        const arr = JSON.parse(JSON.stringify(this.selectcpRow));
        const val = array.some(({
          productNo
        }) => productNo === arr.productNo);
        if (!val) {
          this.ProductForm.mxDataList.push(this.selectcpRow);
        } else {
          this.ProductForm.mxDataList.forEach((e) => {
            if (e.productNo === arr.productNo) {
              e.packageDataList = arr.packageDataList;
            }
          });
        }
        this.statistics();
        this.getExpends();
        this.isSemidialog = false;
      },
      // 获取表格表头
      getTableHead() {
        const parmas = {
          tableCode: this.$config.cpscrapbilllist,
        };
        this.$api.common.getAllTableHead(parmas).then((res) => {
          res.sort(arraySort("columnSort"));
          this.tableData.thead = res
        });
      },
      // 所有列表数据
      getAllMaterils() {
        const parmas = this.search;
        this.searchLoading = true;
        this.$api.warehouse
          .GetMatchPageFinishedProductScrapBills(parmas)
          .then((res) => {
            if (res.list.length > 0) {
              res.list.forEach((item) => {
                item.isVoid = 0;
                if (item.scrapBillState === 1 && item.stockState === 0) {
                  item.isVoid = 1;
                }
              });
            }
            this.tableData.tbody = res.list;
            this.search.total = res.totalCount;
          })
          .catch(() => {
            this.searchLoading = false;
          });
      },
      // 新建弹框关闭事件
      closeDialog() {
        if (this.$refs.ProductForm) {
          this.$refs.ProductForm.resetFields();
        }
        this.ProductForm = this.$options.data().ProductForm;
      },
    },
  };

</script>
<style lang="scss" scoped>
  .container {
    height: 100%;
  }

  .permangeRg {
    border: 1px solid #e6e6e6;
    background: #fff;
    height: 100%;

    .mainTT {
      font-size: 16px;
      font-weight: bold;
      width: 100%;
      padding: 18px 16px;
      padding-bottom: 0;
    }
  }

  .addWuliaoBox {

    // padding: 0px 16px 0 16px;
    .boxleft {
      padding: 5px 5px;
    }

    .boxright {
      height: 100%;
      padding: 5px 5px;
      border-left: 1px solid #eeeeee;
    }

    .tableBox {
      margin-top: 16px;
      border: 1px solid #e6e6e6;
      border-bottom: none;
      border-radius: 8px 8px 0 0;
      overflow: hidden;

      .tt {
        height: 38px;
        line-height: 38px;
        padding-left: 16px;
        background: #f7f8fa;
        border-radius: 8px 8px 0 0;
      }
    }
  }

  .subTip {
    border-bottom: 1px #e6e6e6 solid;
    background: #f5f6f9;
    padding: 8px 16px;
  }

  .dialogExpandTable {
    border-left: 1px solid #e6e6e6;
    border-right: 1px solid #e6e6e6;
    margin: 0 10px;
  }

</style>
